package com.example.educationai.controller;

import com.example.educationai.entity.ClassEntity;
import com.example.educationai.entity.SimpleUser;
import com.example.educationai.service.ClassService;
import com.example.educationai.service.StudentService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/class")
public class ClassController {

    @Autowired
    private ClassService classService;

    @Autowired
    private StudentService studentService;

    @GetMapping("/manage")
    public String showManagePage(Model model, HttpSession session) {
        SimpleUser user = (SimpleUser) session.getAttribute("user");
        if (user == null) {
            return "redirect:/simple-login";
        }
        
        List<ClassEntity> classes = classService.findByTeacherId(user.getId());
        model.addAttribute("classes", classes);
        return "class/manage";
    }

    @PostMapping("/create")
    public String createClass(@RequestParam String className,
                            @RequestParam String grade,
                            HttpSession session,
                            RedirectAttributes redirectAttributes) {
        SimpleUser user = (SimpleUser) session.getAttribute("user");
        if (user == null) {
            return "redirect:/simple-login";
        }

        try {
            ClassEntity classEntity = new ClassEntity();
            classEntity.setClassName(className);
            classEntity.setGrade(grade);
            classEntity.setTeacher(user);
            
            classService.save(classEntity);
            redirectAttributes.addFlashAttribute("success", "班级创建成功！");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", "创建班级失败：" + e.getMessage());
        }
        
        return "redirect:/class/manage";
    }

    @PostMapping("/add-student")
    public String addStudent(@RequestParam Long classId,
                           @RequestParam String studentName,
                           @RequestParam String studentId,
                           HttpSession session,
                           RedirectAttributes redirectAttributes) {
        SimpleUser user = (SimpleUser) session.getAttribute("user");
        if (user == null) {
            return "redirect:/simple-login";
        }

        try {
            studentService.addStudentToClass(classId, studentName, studentId);
            redirectAttributes.addFlashAttribute("success", "学生添加成功！");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", "添加学生失败：" + e.getMessage());
        }
        
        return "redirect:/class/manage";
    }

    @PostMapping("/delete/{id}")
    public String deleteClass(@PathVariable Long id,
                            HttpSession session,
                            RedirectAttributes redirectAttributes) {
        SimpleUser user = (SimpleUser) session.getAttribute("user");
        if (user == null) {
            return "redirect:/simple-login";
        }

        try {
            classService.deleteById(id);
            redirectAttributes.addFlashAttribute("success", "班级删除成功！");
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", "删除班级失败：" + e.getMessage());
        }
        
        return "redirect:/class/manage";
    }
} 